<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Models\CommunityGoods;
use App\Models\CommunityHead;
use App\Models\Groupbooking;
use App\Models\OrderTab;
use App\Service\CommonService;

class GroupController extends Controller
{
    /**
     * User: YKY
     * DateTime: 2022/11/25 9:07
     * @return array
     * 团长端商品详情
     */
    public function inFo()
    {
        $gid = \request()->input();
        try {
            $goods = CommunityGoods::where('id', $gid)->first();
            return CommonService::success(200, '查询成功', $goods);
        } catch (\Exception $e) {
            return CommonService::fail(9999, $e->getMessage(), '');
        }
    }

    /**
     * User: YKY
     * DateTime: 2022/11/23 19:50
     * @return array
     * 获取团长位置
     */
    public function addAddress()
    {
        $uid = (int)\request()->input('uid');
        $accuracy = \request()->input();
        try {
            $group = CommunityHead::where('user_id', $uid)->first();
            if ($group) return CommonService::success(200, '自提点位置', $group);
            $address = [
                'latitude' => $accuracy['latitude'],
                'longitude' => $accuracy['longitude'],
                'address' => $accuracy['address'] . $accuracy['name']
            ];
            CommunityHead::where('user_id', $uid)->update($address);

            return CommonService::success(200, '新自提点位置', $group);
        } catch (\Exception $e) {
            return CommonService::fail(9999, $e->getMessage());
        }
    }

    /**
     * User: YKY
     * DateTime: 2022/11/24 20:01
     * @return array
     * 团长端商品列表
     */
    public function goodsList()
    {
        try {
            $goodsData = CommunityGoods::get();
            return CommonService::success(200, '查询成功', $goodsData);
        } catch (\Exception $e) {

        }
    }

    /**
     * User: YKY
     * DateTime: 2022/11/24 20:02
     * 申请团长
     */
    public function appLy()
    {
        $uid = intval(request()->input('uid'));
        $appLyData = \request()->input();
        $addAppLy = [
            'user_id' => $uid,

        ];
        try {
            CommunityHead::create($addAppLy);
        } catch (\Exception $e) {

        }
    }

    public function groupTab()
    {
        $tab = OrderTab::limit(3)->get();
        return CommonService::success(200, '', $tab);
    }

    /**
     * User: YKY
     * DateTime: 2022/11/25 14:14
     * @return array
     * 拼团列表
     */
    public function groupOrderList()
    {
        $uid = intval(request()->input('uid'));
        $status = intval(request()->input('status'));
        try {
            if (empty($status)) {
                $orderData = Groupbooking::with(['getStatus'])->where(['uid' => $uid])->get();
                return CommonService::success(200, '查询成功', $orderData);
            }
            if ($status == 0) {
                $orderData = Groupbooking::with(['getStatus'])->where(['uid' => $uid])->get();
                return CommonService::success(200, '查询成功', $orderData);
            }
            $group = Groupbooking::with('getStatus')->where(['groupbooking_uid' => $uid, 'groupbooking_stare' => $status])->get();
            return CommonService::success(200, '', $group);
        } catch (\Exception $e) {
            return CommonService::fail(9999, $e->getMessage());
        }
    }
}
